package ru.kisa_wst.HTTP;

import java.io.StringReader;
import java.net.HttpURLConnection;
import java.util.Properties;

public class HTTPHeader {
	private HttpURLConnection http_conn;
	private static final String DEF_ENCODING=System.getProperty("file.encoding"); // default system encoding
	
	/**
	 * Inner method for parse param such par1=val1;par2=val2
	 * @param String params in format par1=val1;par2=val2....
	 * @return {@link Properties}
	 */
	private Properties parseParameter(String params){
		String[] foo=params.split(";");
		Properties properties = new Properties();
		for(String bar:foo){
			try{
				properties.load(new StringReader(bar));
			}
			catch (Exception e) {
				System.err.println("Error in parse param: "+e.getMessage());
			}
		}
		return properties;
	}
	/**
	 * Constructor
	 * @param {@link HttpURLConnection}
	 * 	 
	 * */
	public HTTPHeader(HttpURLConnection http_conn) {
		this.http_conn=http_conn;
		HttpURLConnection.setFollowRedirects(true);
		http_conn.setInstanceFollowRedirects(false);
		
	}
	/**
	 * Simple method to get server name
	 * @return server name extracted from connection 
	 * 	 
	 *  */
	public  String getServerName(){
		return http_conn.getHeaderField("Server");
	}
	/**
	 * Get redirect from http header if exist
	 * @return URI to redirect or null if not redirected 	 
	 *  
	 *  */
	public String getURLIfRedirect(){
		try{
			if(String.valueOf(http_conn.getResponseCode()).startsWith("3")){
				return http_conn.getHeaderField("Location");
			}			
		}
		catch (Exception e) {
			System.err.println("error in getURLIsRedirect: "+e.getMessage());
			return null;
		}
		return null;
	}
	/**
	 * Check page found or not
	 * @return <b>true</b> if page not found	 
	 *  
	 *  */
	public boolean isNotFound(){
		try{
			if(String.valueOf(http_conn.getResponseCode()).contains("404")){
				return true;
			}			
		}
		catch (Exception e) {
			System.err.println("error in isNotFound: "+e.getMessage());
			return false;
		}
		return false;
	}
	/**
	 * Check page forbiden or not
	 * @return <b>true</b> if page forbiden	 
	 *  
	 *  */
	public boolean isForbiden(){
		try{
			if(String.valueOf(http_conn.getResponseCode()).contains("403")){
				return true;
			}			
		}
		catch (Exception e) {
			System.err.println("error in isForbiden: "+e.getMessage());
			return false;
		}
		return false;
	}
	/**
	 * If server set cookie, get this. 
	 * @return String cookie
	 *  
	 *  */
	public String getCookieIfSetCookie(){
		return http_conn.getHeaderField("Set-Cookie");
	}
	/**
	 * Get Encoding of page
	 * @return return page encoding, or system encoding if encoding not found on page.
	 *  
	 *  */
	public String getHTMLCharsetEncoding(){
		String encoding = DEF_ENCODING;
		if (http_conn==null) return encoding;
		String httpEncoding=parseParameter(http_conn.getContentType()).getProperty("charset");
		if(httpEncoding!=null) encoding = httpEncoding;
		return encoding;
	}
	/**
	 * Check page to text content or not
	 * @return return true if page is text
	 *  
	 *  */
	public boolean isTextContent(){
		boolean isText=false;
		String content=http_conn.getContentType();
		if(content==null) return isText;
		if(	content.contains("text/html")||
			content.contains("text/css")||
			content.contains("text/plain")||
			content.contains("text/xml")||
			content.contains("text/csv")||
			content.contains("text/javascript")){
			isText=true;
		}
		return isText;
	}
	public String getWarning(){
		return http_conn.getHeaderField("Warning");
	}
}
